#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

// android effect only support ogg
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    #define EFFECT_FILE        "Sounds/effect2.ogg"
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
    #define EFFECT_FILE        "Sounds/effect1.raw"
#else
    #define EFFECT_FILE        "Sounds/effect1.wav"
#endif // CC_PLATFORM_ANDROID

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    #define MUSIC_FILE        "Sounds/music.mid"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
    #define MUSIC_FILE        "Sounds/background.ogg"
#else
    #define MUSIC_FILE        "Sounds/background.mp3"
#endif // CC_PLATFORM_WIN32

std::string menuItems[] = {
    "play background music",
    "pause background music",
    "resume background music",
    "stop background music",
    "play effect",
    "play effect repeatly",
    "pause effect",
    "resume effect",
    "stop effect",
};

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    
    HelloWorld *layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )
    {
        return false;
    }

    /////////////////////////////

    // 메뉴 아이템 생성 및 초기화

    CCMenu* pMenu = CCMenu::create();
    
    int nMenuCount = sizeof(menuItems) / sizeof(menuItems[0]);
    
    for (int i = 0; i < nMenuCount; ++i)
    {
        CCLabelTTF* label = CCLabelTTF::create(menuItems[i].c_str(), "Arial", 24);
        label->setColor(ccc3(0, 0, 0));
        CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::doSoundAction));
        
        pMenu->addChild(pMenuItem, i + 10000);
    }

    
    // 메뉴 정렬
    pMenu->alignItemsVertically();
    
    // 레이어에 메뉴 객체 추가
    this->addChild(pMenu);


    
    
    // preload background music and effect
    SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
    SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
    
    // set default volume
    SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
    SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);

    
    return true;
}

void HelloWorld::onExit()
{
    
    SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
    SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true);
    
    SimpleAudioEngine::sharedEngine()->end();

    CCLayer::onExit();
}

void HelloWorld::doSoundAction(CCObject* pSender)
{
    CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
    int nIdx = pMenuItem->getZOrder() - 10000;
    
    switch(nIdx)
    {
            // play background music
        case 0:
            SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);
            break;
            // stop background music
            // pause background music
        case 1:
            SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
            break;
            // resume background music
        case 2:
            SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
            break;
        case 3:
            SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
            break;
            // play effect
        case 4:
            m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
            break;
            // play effect
        case 5:
            m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
            break;
            // pause effect
        case 6:
            SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
            break;
            // resume effect
        case 7:
            SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
            break;
            // stop effect
        case 8:
            SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
            break;
            
    }
    
}



